Directly join owned threads on cleanup#92
Open
GregoryComer wants to merge 1 commit intogoogle:mainfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Windows during process shutdown, Windows can kill the threadpool threads before they have a chance to signal exit. This causes pthreadpool_destroy to hang indefinitely in wait_on_num_recruited_threads. I ran into this while updating pytorch and executorch to use google/pthreadpool, in preparation to update the XNNPACK dependency.
Here's a backtrace for the hang. Note that all threadpool threads are already killed.
To repro, having a pthreadpool destructor called at process exit on Windows will repro 100% of the time for me (for example, from a destructor on a static global). I'm using clang-cl and running inside a native python extension, I haven't tried MSVC.
To solve it, I've just reworked the non-executor thread path to just directly join. It will still call wait_on_num_recruited threads when using executor-provided threads.
In theory, executor-owned threads could have the same bug. It should maybe have a timeout. Alternatively, if you wanted to tolerate some windows-specific code, you could check to see if the thread is still alive.
Test Plan
I verified that this patch works on PyTorch core and ExecuTorch. There's a few red jobs, but they are unrelated.
pytorch/pytorch#178201
pytorch/executorch#19237